package com.wkck.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkck.domain.Delivered_Library;
import com.wkck.domain.Job;
import com.wkck.domain.Resume;
import com.wkck.mapper.JobMapper;
import com.wkck.service.JobService;
import com.wkck.utils.Code;
import com.wkck.utils.Msg;
import com.wkck.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/jobs")
@Slf4j
public class JobController {

    @Resource
    private JobService jobService;

    /**
     * 得到页面的简易信息
     * @return
     */

    @GetMapping("/easyList")
    @Cacheable(value = "job_easyMsg")
    public Result getEasyMsg(){
        return new Result(Code.SELECT_OK,jobService.getEasyMsg(), Msg.SELECT_OK);
    }

    /**
     * 不携带任何参数的分页查询功能
     * @param currentPage
     * @param pageSize
     * @return
     */

    @GetMapping("{currentPage}/{pageSize}")
    @Cacheable(value = "job_AllMsg")
    public Result getAllMsg(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Job> page = jobService.getPage(currentPage,pageSize);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = jobService.getPage((int)page.getPages(), pageSize);
        }

        long count = page.getTotal() - jobService.countJobLock();
        page.setTotal(count < 0 ? 0 : count);

        return new Result(Code.SELECT_OK,page,Msg.SELECT_OK);
    }

    /**
     * 携带参数的分页查询功能
     * @param currentPage
     * @param pageSize
     * @param job
     * @return
     */

    @GetMapping("/like/{currentPage}/{pageSize}")
    @Cacheable(value = "job_AllMsgParams")
    public Result getAllMsgAndParams(@PathVariable int currentPage, @PathVariable int pageSize, Job job){
        IPage<Job> page = jobService.getPageAndParams(currentPage,pageSize,job);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > page.getPages()){
            page = jobService.getPageAndParams((int)page.getPages(), pageSize,job);
        }

        long count = page.getTotal() - jobService.countJobLock();
        page.setTotal(count < 0 ? 0 : count);

        return new Result(Code.SELECT_OK,page,Msg.SELECT_OK);
    }

    /**
     * 根据id来获取该职位的详细信息
     * @param id
     * @return
     */

    @GetMapping("/getAllInformation/{id}")
    @Cacheable(value = "JobController",key = "#id")
    public Result list(@PathVariable Integer id){
        return new Result(Code.SELECT_OK,jobService.getAll(id),Msg.SELECT_OK);
    }

    /**
     * 根据用户的id来判断是否有该用户的简历信息
     * 如果没有则返回null
     * @param id
     * @return
     */

    @GetMapping("/getResumeByUserId/{id}")
    @Cacheable(value = "ResumeMsgByUserId",key = "#id")
    public Result getResumeByUserId(@PathVariable Integer id){
        Resume userResumeMsg = jobService.getResumeByUserId(id);
        log.info("得到用户的信息成功"+userResumeMsg);
        Integer code = userResumeMsg == null ? Code.SELECT_OK : Code.SELECT_ERR;
        String msg = userResumeMsg == null ? Msg.SELECT_OK : Msg.SELECT_ERR;
        return new Result(code,userResumeMsg,msg);
    }

    /**
     * 发送简历
     * @param job_join_id
     * @param user_id
     * @param resume_id
     * @return
     */

    @PostMapping("/sendResume/{job_join_id}/{user_id}/{resume_id}")
    public Result insertSendResumeMessage
            (
                    @PathVariable Integer job_join_id,
                    @PathVariable Integer user_id,
                    @PathVariable Integer resume_id
            ){
        //生效行数
        int i = jobService.insertSendResumeMessage(job_join_id, user_id, resume_id);
        Integer code = i == 1 ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = i == 1 ? Msg.SAVE_OK : Msg.SAVE_ERR;
        return new Result(code,i,msg);
    }

    /**
     * 发送简历的判断
     * @param job_join_id
     * @param user_id
     * @param resume_id
     * @return
     */

    @GetMapping("/getResume/{job_join_id}/{user_id}/{resume_id}")
    public Result selectResumeAndCompany
            (
                    @PathVariable Integer job_join_id,
                    @PathVariable Integer user_id,
                    @PathVariable Integer resume_id
            ){

        Delivered_Library deliveredLibrary = jobService.selectResumeAndCompany(job_join_id, user_id, resume_id);

        return new Result(Code.SELECT_OK,deliveredLibrary,Msg.SELECT_OK);
    }

    /**
     * 后台首页饼图数据
     * @return
     */
    @GetMapping("/pieChartInfo")
    @Cacheable(value = "getPieChartInfo")
    public Result getPieChartInfo(){
        return new Result(Code.SELECT_OK,jobService.getJobAddressInfo(),Msg.SELECT_OK);
    }

    /**
    * @Author: CK
    * @Date: 2023/12/29
    * @Param: []
    * @return:
    * @Description:
    */
    @GetMapping("/getCities")
    public com.wkck.result.Result<List<String>> getCities() {
        return com.wkck.result.Result.success(jobService.getCities());
    }
}
